package com.jzr.board.db1.service.Impl;


import com.jzr.board.db1.mapper.MyWorkViewMapper;
import com.jzr.board.db1.service.TWorkViewService;
import com.jzr.board.db1.vo.NurseServiceSumVo;
import com.jzr.board.db1.vo.NurseServiceVo;
import com.jzr.board.db1.vo.OutlayTypeSumVo;
import com.jzr.board.db1.vo.WorkViewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class TWorkViewServiceImpl implements TWorkViewService {
    @Autowired
    private MyWorkViewMapper workViewMapper;

    @Override
    public WorkViewVo get(String deptCode) {
        WorkViewVo workViewVo = new WorkViewVo();
        LocalDate today = LocalDate.now();
        LocalDate nextDay = today.plusDays(1);
        LocalDate nextDay2 = today.plusDays(2);

        Integer qty = 0;

        //住院总数
        workViewVo.setTotalInPat(workViewMapper.getTotalInPat(deptCode));
        //今日入院数
        workViewVo.setTodayIn(workViewMapper.getInQty(deptCode,today,nextDay));
        //明日出院数
        workViewVo.setNextDayOut(workViewMapper.getOutQty(deptCode,nextDay,nextDay2));
        //重症病人数
        workViewVo.setSevereCare(workViewMapper.getSevereCareQty(deptCode));
        //费用类别统计
        List<OutlayTypeSumVo> outlayTypeSumVoList = workViewMapper.getOutlayTypeSum(deptCode);
        if(outlayTypeSumVoList != null) {
            for(OutlayTypeSumVo outlay : outlayTypeSumVoList){
                if(outlay.getType() != null) {
                    if (outlay.getType().intValue() == 1) { //社保
                        workViewVo.setSocialsPay(outlay.getPatQty());
                    }else if(outlay.getType().intValue() == 2){ //自费
                        workViewVo.setSelfPay(outlay.getPatQty());
                    }else if(outlay.getType().intValue() == 3){ //新农合
                        workViewVo.setNewRuralPay(outlay.getPatQty());
                    }
                }
            }
        }
        //今日手术数
        workViewVo.setTodayOp(workViewMapper.getOperationQty(deptCode,today,nextDay));
        //明天手术数
        workViewVo.setNextDayOp(workViewMapper.getOperationQty(deptCode,nextDay,nextDay2));
        //今日转床数
        workViewVo.setTodayTranBed(workViewMapper.getTranBedQty(deptCode,today,nextDay));
        //转入数
        workViewVo.setTranIn(workViewMapper.getTranInQty(deptCode,today,nextDay));
        //转出数
        workViewVo.setTranOut(workViewMapper.getTranOutQty(deptCode,today,nextDay));
        //转床列表
        workViewVo.setTranBeds(workViewMapper.getTranBedList(deptCode,today,nextDay));
        //护理级别统计
        workViewVo.setNurseLevelSums(workViewMapper.getNurseLevelSum(deptCode));
        //护理信息统计
        List<NurseServiceVo> nurseServiceVoList = workViewMapper.getNurseService(deptCode);
        List<NurseServiceSumVo> nurseServiceSumVoList = workViewMapper.getNurseServiceSum();
        if(nurseServiceSumVoList != null && nurseServiceVoList != null && !nurseServiceVoList.isEmpty()){
            for(NurseServiceSumVo a : nurseServiceSumVoList){
                if(a.getCode() != null) {
                    List<String> list = new ArrayList<>();
                    for(NurseServiceVo b : nurseServiceVoList){
                        if(b.getCode() != null && a.getCode().equalsIgnoreCase(b.getCode())){
                            list.add(b.getBedName());
                        }
                    }
                    if(!list.isEmpty()){
                        a.setBeds(list);
                    }
                }
            }
        }
        workViewVo.setNurseServiceSums(nurseServiceSumVoList);
        return workViewVo;
    }

}
